//
//  ViewController.m
//  Exam10-1
//
//  Created by 孙晓晔 on 2017/4/11.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "Lyric.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *name; // 歌名
@property (weak, nonatomic) IBOutlet UILabel *content; // 歌词
@property (weak, nonatomic) IBOutlet UILabel *timeCurrent; // 当前时间

@property (weak, nonatomic) IBOutlet UILabel *timeTotal; // 总时长

@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIButton *playBtn; // 播放
@property (weak, nonatomic) IBOutlet UIButton *pauseBtn; // 暂停
@property (weak, nonatomic) IBOutlet UIButton *stopBtn; // 停止

@property(nonatomic,strong) AVAudioPlayer *player;
@property(nonatomic,strong) NSTimer *timer1; // 定时器

@property(nonatomic, strong) Lyric *lyric;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _lyric = [[Lyric alloc] init:@"我从雪山来" ofType:@"lrc"];
    
    self.name.text = self.lyric.name;
    
    
    [self setUp];
}

-(void)setUp {
    self.pauseBtn.enabled = FALSE;
    self.stopBtn.enabled = FALSE;
}

// 定时器开
-(void)timerOn {
    self.timer1 = [NSTimer scheduledTimerWithTimeInterval:0.3 repeats:YES block:^(NSTimer * _Nonnull timer) {
        self.slider.value = self.player.currentTime / self.player.duration;
        
        NSString *tmp = [self.lyric lyricByTime:self.player.currentTime];
        
        if ([tmp length] > 0) {
            self.content.text = tmp;
        }
        
        self.timeCurrent.text = [self formatTime:self.player.currentTime];
        self.timeTotal.text = [self formatTime:self.player.duration];
        
    }];
}

// 时间格式化
-(NSString*)formatTime:(NSTimeInterval)time {
    NSInteger second = (NSInteger)time;
    
    NSInteger minute = 0;
    if (second > 60) {
        minute = second / 60;
        second = second % 60;
    }
    
    NSString *ret = [NSString stringWithFormat:@"%02ld:%02ld", minute, second];
    
    return ret;
}

// 定时器关
-(void)timerOff {
    [self.timer1 invalidate];
    self.timer1 = nil;
}

// 懒加载
-(AVAudioPlayer *)player {
    
    if (_player == nil) {
        NSString *strPath = [[NSBundle mainBundle] pathForResource:@"我从雪山来" ofType:@"mp3"];
        
        NSURL *url = [NSURL fileURLWithPath:strPath];
        
        NSError *err;
        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
        
        [_player prepareToPlay];
    }
    
    return _player;
}

- (IBAction)slider:(UISlider *)sender {
    self.player.currentTime = self.player.duration * self.slider.value;
}

// 播放
- (IBAction)play:(UIButton *)sender {
    sender.enabled = FALSE;
    self.pauseBtn.enabled = TRUE;
    self.stopBtn.enabled = TRUE;
    
    [self timerOn];
    [self.player play];
}

// 暂停
- (IBAction)pause:(UIButton *)sender {
    sender.enabled = FALSE;
    self.stopBtn.enabled = TRUE;
    self.playBtn.enabled = TRUE;
    
    [self timerOff];
    
    [self.player pause];
}

// 停止
- (IBAction)stop:(UIButton *)sender {
    sender.enabled = FALSE;
    self.pauseBtn.enabled = FALSE;
    self.playBtn.enabled = TRUE;
    
    [self timerOff];
    
    [self.player stop];
    
    self.player.currentTime = 0;
    self.slider.value = 0;
    self.timeCurrent.text = @"00:00";
    self.timeTotal.text = @"00:00";
    self.content.text = @"";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
